"""
Case Type   : 系统视图PG_PREPARED_STATEMENT
Case Name   : JDBC执行预编译SQL语句，传入session id查询函数
Create At   : 2023/12/14
Owner       : ningyali
Description :
    1.调用java脚本执行以下步骤
        (1)创建10个JDBC连接
        (2)每个连接执行预编译SQL语句
        (3)查询conn连接session id,传入session_id查询函数
        (4)关闭10个JDBC连接
        (5)再次查询
    2.清理环境
Expect      :
    1.执行成功
        (1)执行成功
        (2)执行成功
        (3)查询成功，可查询到第2步的预编译语句
        (4)执行成功
        (5)查询成功，不能查询到第2步的预编译语句
    2.执行成功
History     :
"""

import os
import subprocess
import unittest

from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class PgPreparedStatement40(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.t_name = 't_prepare_40'
        self.java_fname = 'pg_prepared_statement_case0040.java'
        self.jar_name, self.jar_path = self.common.get_jdbc('opengauss')
        self.conn_num = 10

    def test_1(self):
        text = 'step1:调用java脚本执行(1)~(5)步骤; expect:执行成功'
        self.log.info(f'-----{text}-----')
        self.log.info(f'-----拷贝java脚本到可执行路径-----')
        java_fpath = os.path.join(macro.SCRIPTS_PATH, 'SystemView',
                                  self.java_fname)
        cmd = f'cp {java_fpath} {os.path.dirname(self.jar_path)}'
        self.log.info(cmd)
        res = subprocess.getoutput(cmd)
        self.log.info(res)
        self.assertEqual(len(res.strip()), 0, f'执行失败：{text}')

        self.log.info(f'-----编译java脚本-----')
        cmd = f'cd {os.path.dirname(self.jar_path)};' \
            f'javac -encoding utf-8 -cp {self.jar_name} {self.java_fname}'
        self.log.info(cmd)
        res = subprocess.getoutput(cmd)
        self.log.info(res)
        self.assertEqual(len(res.strip()), 0, f'执行失败：{text}')

        self.log.info(f'-----执行编译后脚本-----')
        cmd = f'cd {os.path.dirname(self.jar_path)};' \
            f'java -cp {self.jar_name}:./ {self.java_fname.split(".")[0]} ' \
            f'{self.pri_sh.node.db_host} {self.pri_sh.node.db_port} ' \
            f'{self.pri_sh.node.db_name} {self.pri_sh.node.db_user} ' \
            f'{self.pri_sh.node.db_password} {self.conn_num}'
        self.log.info(cmd)
        res = subprocess.getoutput(cmd)
        self.log.info(res)
        expect = 'select count(*) from (select pg_prepared_statement(' \
                 'sessionid) from pg_stat_activity);\n' \
                 '----查询结果----\n' \
                 'count\n' \
                 '10\n' \
                 '----关闭测试连接----\n' \
                 '----主连接再次查询----\n' \
                 'select pg_sleep(1);\n' \
                 '----查询结果----\n' \
                 'pg_sleep\n\n' \
                 'select pg_prepared_statement(sessionid) from ' \
                 'pg_stat_activity;\n' \
                 '----查询结果----\n' \
                 'pg_prepared_statement\n' \
                 'select count(*) from (select pg_prepared_statement(' \
                 'sessionid) from pg_stat_activity);\n' \
                 '----查询结果----\n' \
                 'count\n' \
                 '0'
        self.assertIn(expect, res, f'执行失败：{text}')
        self.assertNotIn('error', res.lower(), f'执行失败：{text}')

    def tearDown(self):
        self.log.info('----run teardown----')
        text = 'step2:清理环境; expect:执行成功'
        self.log.info(f'-----{text}-----')
        f_path = os.path.join(os.path.dirname(self.jar_path),
                              self.java_fname.split(".")[0]+"*")
        rm_cmd = f'rm -rf {f_path}'
        self.log.info(rm_cmd)
        rm_res = subprocess.getoutput(rm_cmd)
        self.log.info(rm_res)
        self.assertEqual(len(rm_res.strip()), 0, f'执行失败：{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
